<template>
  <el-form :model="collectionForm" class="collectionForm">
    <el-form-item>
      <el-col :span="2" style="text-align: end">
        <label>{{ collectionForm.name }}</label>
      </el-col>
      <el-col :span="22">
        <el-col :span="24">
          <el-radio v-model="collectionForm.recoveryFlatExpressType" label="0" :disabled="disabled">物流回收</el-radio>
        </el-col>
        <el-col :span="24">
          <el-col :span="4">
            <el-select v-model="collectionForm.recoveryExpressId" placeholder="请选择" :disabled="disabled ||(!disabled&&collectionForm.recoveryFlatExpressType=='1')">
              <el-option
                label="顺丰"
                value="0"
                selected
              >
                顺丰
              </el-option>
            </el-select>
          </el-col>
          <el-col :span="6">
            <el-input v-model="collectionForm.recoveryExpressCost" placeholder="输入费用" :disabled="disabled ||(!disabled&&collectionForm.recoveryFlatExpressType=='1')" @keyup="changeRecoveryExpressCost" />
          </el-col>
        </el-col>
        <el-col :span="24">
          <el-radio v-model="collectionForm.recoveryFlatExpressType" label="1" :disabled="disabled">同城自营回收</el-radio>
        </el-col>
        <el-col :span="24">
          <el-col :span="6">
            <el-input v-model="collectionForm.recoveryByselfCost" placeholder="输入费用" :disabled="disabled ||(!disabled&&collectionForm.recoveryFlatExpressType=='0')" @keyup="changeRecoveryByselfCost" />
          </el-col>
        </el-col>
      </el-col>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  name: 'CloudStoreExpress',
  props: {
    collection: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      collectionForm: {
        id: null,
        name: null,
        recoveryFlatExpressType: '0',
        recoveryExpressId: '0',
        recoveryExpressCost: null,
        recoveryByselfCost: null
      },
      disabled: false
    }
  },
  created() {
    this.collectionForm.id = this.collection.id
    this.collectionForm.name = this.collection.name
  },
  methods: {
    showDialogYF(number) {

    },
    changeDisabled(disabled) {
      this.disabled = disabled
    },
    setData(list) {
      if (list.length > 0) {
        list.forEach(item => {
          console.log(item)
          if (item.id == this.collection.id) {
            this.collectionForm.recoveryFlatExpressType = item.recoveryFlatExpressType.toString()
            if (item.recoveryFlatExpressType == 0) {
              this.collectionForm.recoveryExpressCost = item.recoveryFlatExpressCost
              this.collectionForm.recoveryExpressId = item.recoveryExpressId.toString()
            } else if (item.recoveryFlatExpressType == 1) {
              this.collectionForm.recoveryByselfCost = item.recoveryFlatExpressCost
            }
          }
        })
      }
    },
    changeRecoveryExpressCost() {
      this.collectionForm.recoveryExpressCost = this.collectionForm.recoveryExpressCost.replace(/[^0-9.]/g, '')
    },
    changeRecoveryByselfCost() {
      this.collectionForm.recoveryByselfCost = this.collectionForm.recoveryByselfCost.replace(/[^0-9.]/g, '')
    },
    getData() {
      if (!this.collectionForm.recoveryFlatExpressType) {
        return this.$message.error('请选择回收方式')
      }
      if (this.collectionForm.recoveryFlatExpressType == '0') {
        if (!this.collectionForm.recoveryExpressId) {
          return this.$message.error('请选择回收物流公司')
        }
        if (!this.collectionForm.recoveryExpressCost) {
          return this.$message.error('请输入回收运费')
        } else if (!/^((\d|[1-9]\d+)|\.\d{1,2}|(\d|[1-9]\d+)\.\d{1,2})$/g.test(this.collectionForm.recoveryExpressCost)) {
          return this.$message.error('回收运费格式不正确')
        } else if ((/^((\d|[1-9]\d+)|\.\d{1,2}|(\d|[1-9]\d+)\.\d{1,2})$/g.test(this.collectionForm.recoveryExpressCost) && this.collectionForm.recoveryExpressCost > 100000)) {
          return this.$message.error('回收运费不能大于100000')
        } else if (/^((\d|[1-9]\d+)|\.\d{1,2}|(\d|[1-9]\d+)\.\d{1,2})$/g.test(this.collectionForm.recoveryExpressCost) && this.collectionForm.recoveryExpressCost <= 0) {
          return this.$message.error('回收运费必须大于0')
        }
      } else if (this.collectionForm.recoveryFlatExpressType == '1') {
        if (!this.collectionForm.recoveryByselfCost) {
          return this.$message.error('请输入回收运费')
        } else if (!/^((\d|[1-9]\d+)|\.\d{1,2}|(\d|[1-9]\d+)\.\d{1,2})$/g.test(this.collectionForm.recoveryByselfCost)) {
          return this.$message.error('回收运费格式不正确')
        } else if ((/^((\d|[1-9]\d+)|\.\d{1,2}|(\d|[1-9]\d+)\.\d{1,2})$/g.test(this.collectionForm.recoveryByselfCost) && this.collectionForm.recoveryByselfCost > 100000)) {
          return this.$message.error('回收运费不能大于100000')
        } else if (/^((\d|[1-9]\d+)|\.\d{1,2}|(\d|[1-9]\d+)\.\d{1,2})$/g.test(this.collectionForm.recoveryByselfCost) && this.collectionForm.recoveryByselfCost <= 0) {
          return this.$message.error('回收运费必须大于0')
        }
      }
      const data = this.$XEUtils.clone(this.collectionForm, true)
      delete data.name
      let recoveryFlatExpressCost = null
      if (data.recoveryFlatExpressType == '0') {
        recoveryFlatExpressCost = data.recoveryExpressCost
      } else if (data.recoveryFlatExpressType == '1') {
        recoveryFlatExpressCost = data.recoveryByselfCost
      }
      data['recoveryFlatExpressCost'] = recoveryFlatExpressCost
      delete data.recoveryByselfCost
      delete data.recoveryExpressCost
      return data
    }
  }
}
</script>

<style lang="scss" scoped>
.collectionForm {
  .el-form-item__content {
    margin-left: 0;
  }
}
</style>
